<template>
  <el-form ref="form" :label-position="device === 'mobile' ? 'top' : 'right'" label-width="100px" :model="form"
    :rules="rules">
    <el-form-item label="姓名" prop="name">
      <el-input v-model.trim="form.name" autocomplete="off" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="入职年龄" prop="age">
      <el-input v-model.number="form.age" autocomplete="off" placeholder="请输入入职年龄" type="number" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model.trim="form.phone" autocomplete="off" placeholder="请输入手机号" type="number" />
    </el-form-item>
    <el-form-item label="身份证号码" prop="idCard">
      <el-input v-model.trim="form.idCard" autocomplete="off" placeholder="请输入身份证号码" type="number" />
    </el-form-item>
    <el-form-item label="聘用开始时间" prop="startTime">
      <el-date-picker v-model="form.startTime" placeholder="请选择聘用结束时间" style="width: 100%" value-format="timestamp" />
    </el-form-item>
    <el-form-item label="聘用结束时间" prop="endTime">
      <el-date-picker v-model="form.endTime" placeholder="请选择聘用结束时间" style="width: 100%" value-format="timestamp" />
    </el-form-item>
    <el-form-item label="实际离职时间" prop="turnoverTime">
      <el-date-picker v-model="form.turnoverTime" placeholder="请选择实际离职时间" style="width: 100%" value-format="timestamp" />
    </el-form-item>
    <el-form-item label="负责圈舍" prop="penHouse">
      <el-input v-model.trim="form.penHouse" autocomplete="off" placeholder="请输入负责圈舍" />
    </el-form-item>
    <el-form-item label="其他工作" prop="otherWork">
      <el-input v-model.trim="form.otherWork" autocomplete="off" placeholder="请输入其他工作" :rows="2" type="textarea" />
    </el-form-item>
    <el-form-item label="工作表现" prop="performance">
      <el-input v-model.trim="form.performance" autocomplete="off" placeholder="请输入工作表现" :rows="2" type="textarea" />
    </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex";
import { API_userCreate, API_userEdit } from "@/api/user";
export default {
  name: "FormEdit",
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        name: "",
        age: "",
        phone: "",
        idCard: "",
        startTime: "",
        endTime: "",
        turnoverTime: "",
        penHouse: "",
        otherWork: "",
        performance: "",
        password: "123456",
      },
      rules: {
        name: [{ required: true, trigger: "blur", message: "请输入姓名" }],
        phone: [{ required: true, trigger: "blur", message: "请输入手机号" }],
        idCard: [
          { required: true, trigger: "blur", message: "请输入身份证号码" },
        ],
        startTime: [{ required: true, trigger: "blur", message: "请选择聘用开始时间" }],
        endTime: [{ required: true, trigger: "blur", message: "请选择聘用结束时间" }],
      },
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  watch: {
    formData: {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData);
        }
      },
      immediate: true,
    },
  },
  methods: {
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      if (this.device === "mobile") {
        this.$router.push({
          name: "User",
          params: { refresh: true },
        });
      }
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.form.startTime > this.form.endTime) {
            return this.$baseMessage('聘用开始时间不能大于聘用结束时间', 'error')
          }
          if (this.form.code) {
            const { message } = await API_userEdit(this.form);
            this.$baseMessage(message, "success");
          } else {
            const { message } = await API_userCreate(this.form);
            this.$baseMessage(message, "success");
          }
          this.$emit("fetch-data");
          this.close();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
